<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class LettermanModelCampaigns extends JModel
{

	var $_data = null;

	var $_total = null;
        
        var $_pagination = null;

	function __construct()
	{
		parent::__construct();
                
		global $mainframe, $option;                

		// Get the pagination request variables
		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart	= $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

		// In case limit has been changed, adjust limitstart accordingly
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	function getData()
	{
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}
        
        function _buildWhere ()
        {
                global $mainframe, $option;
                
                $search = $mainframe->getUserStateFromRequest( $option.'search','search','','string' );
                $search = JString::strtolower( $search );
                
                $where = array();
                
                if ($search) {
                        $where[] = 'LOWER(c.title) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
                }           
               
                $where = ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
                
                return $where;
        }
        
	function _buildOrder()
	{
		global $mainframe, $option;

		$filter_order = $mainframe->getUserStateFromRequest( $option.'filter_order', 'filter_order', 'c.title', 'cmd' );
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir','', 'word' );

                //$filter_order = "c.title";
		$orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir ;

		return $orderby;
	}           

	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	function _buildQuery()
	{
		$user =& JFactory::getUser();
		$aid = $user->get('aid', 0);
                        
                $query = "SELECT c.*, "
                        . "COUNT(s.id) AS subscribers, "
                        . "COUNT(n.id) AS letters, "
                        . "MAX(n.sent) AS last_sent "
                        . "FROM jos_letterman_campaigns AS c "
                        . "LEFT JOIN jos_letterman_subscribers AS s ON s.id = c.id "
                        . "LEFT JOIN jos_letterman AS n ON n.campaign = c.id "
                        . $this->_buildWhere()
                        . " GROUP BY c.id " 
                        . $this->_buildOrder();
                        
		return $query;
	}
        
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}        
}
?>